package com.cxh.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.common.dao.BaseDao;
import com.common.entity.A_goodsState;
import com.cxh.dao.A_goodsStateDao;

public class A_goodsStateDaoImpl extends BaseDao<A_goodsState> implements A_goodsStateDao {

	@Override
	public A_goodsState findById(int id) {
		String sql = " select * from A_goodsState where goodsState_id = ? ";
		Object[] obj = {id};
		List<A_goodsState> lg = findBySql(sql, obj);
		return (lg!=null&&lg.size()>0)?lg.get(0):null;
	}

	@Override
	public int updateA_goods(A_goodsState gs) {
		String sql = "update A_goodsState set goodsState_name=? where goodsState_id=?";
		Object[] objs = {gs.getGoodsState_name(),gs.getGoodsState_id()};
		return executeUpdate(sql, objs);
	}

	@Override
	public int deleteA_goods(int id) {
		String sql = "delete from A_goodsState where goodsState_id=?";
		Object[] objs = {id};
		return executeUpdate(sql, objs);
	}

	@Override
	public int addA_goods(A_goodsState gs) {
		String sql = "insert into A_goodsType values(?,?)";
		Object[] objs = {gs.getGoodsState_id(),gs.getGoodsState_name()};
		return executeUpdate(sql, objs);
	}

	@Override
	public A_goodsState getEntity(ResultSet rs) {
		A_goodsState gs = new A_goodsState();
		try {
			gs.setGoodsState_id(rs.getInt("goodsState_id"));
			gs.setGoodsState_name(rs.getString("goodsState_name"));
			return gs;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	/**
	 * @param Integer goodsState_id, String goodsState_name
	 */
	public List<A_goodsState> findByCondition(Map<String, Object> mp) {
		List<Object> pList = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" select * from A_goodsState where 1=1 ");
		if(!(mp==null||mp.size()<1))
			for(Entry<String, Object> et:mp.entrySet()){
				sb.append("and "+et.getKey()+" = ? ");
				pList.add(et.getValue());
			}
		return findBySql(sb.toString(), pList.toArray());
	}

}
